package com.itheima.reggie.controller;

import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;



/**
 * @author gakki
 * @create 2022/5/6 20:31
 */
@Slf4j
@RestController
@RequestMapping("/data")
public class DataController {

    @Autowired
    private OrdersService ordersService;


    /**
     * 统计每个月的外卖数量及营业额
     * @return
     */
    @GetMapping("/countByMonth")
    public Result<List<Map>> countByMonth() {
        List<Map> maps = ordersService.countBymonth();
        System.out.println(maps);
        return Result.success(maps);
    }

    /**
     * xlsx文件导出
     * @param response
     * @param dishDtoList
     * @return
     * @throws IOException
     */
    @PostMapping("/downloadXlsxByPOI")
    public Result<String> downloadXlsxByPOI(HttpServletResponse response, @RequestBody List<DishDto> dishDtoList) throws IOException {
        log.info("{}",dishDtoList);
        //创建一个全新的xlsx文件
        XSSFWorkbook workbook = new XSSFWorkbook();
       /* //表头样式
        CellStyle cellStyle = workbook.createCellStyle();
        //水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
        //垂直居中
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        //设置自动换行
        cellStyle.setWrapText(true);*/

        //创建全新的sheet工作表
        XSSFSheet sheet = workbook.createSheet();
        //        设置列宽
        sheet.setColumnWidth(0,30*256);
        sheet.setColumnWidth(1,45*256);
        sheet.setColumnWidth(2,15*256);
        sheet.setColumnWidth(3,15*256);
        sheet.setColumnWidth(4,15*256);
        sheet.setColumnWidth(5,45*256);
        //设置标题行
        String[] title = new String[]{"菜品名称", "图片", "菜品分类", "售价", "售卖状态", "最后操作时间"};
        //创建标题行
        XSSFRow titleRow = sheet.createRow(0);
        Cell cell = null;

        //处理标题
        for (int i = 0; i < title.length; i++) {
            cell = titleRow.createCell(i);
            cell.setCellValue(title[i]);
        }

        int rowIndex = 1;
        //处理数据
        for (DishDto dishDto : dishDtoList) {
            //创建行
            XSSFRow row = sheet.createRow(rowIndex);
            //菜品名
            row.createCell(0).setCellValue(dishDto.getName());
            //菜品图片
            row.createCell(1).setCellValue(dishDto.getImage());
            //菜品分类
            row.createCell(2).setCellValue(dishDto.getCategoryName());
            //菜品售价
            row.createCell(3).setCellValue(dishDto.getPrice().intValue());
            //售卖状态
            row.createCell(4).setCellValue(dishDto.getStatus());
            //更新时间
            row.createCell(5).setCellValue(dishDto.getUpdateTime().toString());
            //行数加一
            rowIndex++;
        }

        //导出的文件名称
        String fileName = "菜品管理" +LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")) +".xlsx";
        //设置文件的打开方式和mime类型
        response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), StandardCharsets.UTF_8));
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //ServletOutputStream outputStream = response.getOutputStream();

        workbook.write(new FileOutputStream("D:/" + fileName));
        workbook.close();

        return Result.success("报表导出成功");

    }
}
